package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.CodeMsg;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.entity.dto.DeptDTO;
import com.enbatis.mybatisplugs.entity.vo.SysDeptVO;
import com.enbatis.mybatisplugs.entity.vo.SysResourceVO;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.SysDept;
import com.enbatis.mybatisplugs.service.SysDeptService;


import java.util.List;

@RestController
@RequestMapping("v1/sys_dept")
public class SysDeptController extends BaseController {

@Autowired
private SysDeptService sysDeptService;


    @PostMapping("/add")
    public ResultReturn<Object> add(@RequestBody SysDept sysDept){
        if (StringUtils.isBlank(sysDept.getName())){
           return failed("部门名称不能为空");
        }
        if (sysDept.getPid()==null){
            sysDept.setPid(0L);
        }

        sysDept.setUname(getAccount().getUsername());
        sysDept.setUid(getAccount().getId());
        return  success(null!=sysDeptService.insert(sysDept));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(sysDeptService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody SysDept sysDept){
        sysDept.setId(id);
        return success(sysDeptService.updateById(sysDept));
    }


    @GetMapping("/{id}")
    public ResultReturn<SysDept> get(@PathVariable("id") Long id) {
        return success(sysDeptService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<SysDept>> getList(SysDept sysDept){
        return success(sysDeptService.list(new Wrapper<>(sysDept)));
    }


    @PostMapping("/get_dept_dto_list")
    public ResultReturn<List<DeptDTO>> getDeptDTOList(@RequestBody SysDept sysDept){
        return success(sysDeptService.getDeptDTOList(sysDept));
    }


    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page_vo")
    public ResultReturn<Pages<SysDeptVO>> getPage(@RequestBody SysDeptVO sysDeptVO){
        Page<SysDeptVO> page= getPage();
        return success(sysDeptService.pageVO(page,sysDeptVO));
    }

}
